If you want to automate service control, but you
want to also control the startup type, you need to go beyond the
command line and create scripts that manage your services. Windows
Management Instrumentation (WMI) has a class called Win32_Service
that represents a Windows service. You can return an instance of this
class to work with a specific service on Windows 7. After you have the
service object, you can query its current status with the State property; determine whether the service is running with the Started property; and return the service’s startup type with the StartMode property. You can also change the service state using the StartService, StopService, PauseService, and ResumeService methods.
Listing 1 presents a script that uses most of these properties and methods.
Listing 1. A WMI Script That Toggles a Service’s State Between Started and Stopped
Option Explicit Dim strComputer, strServiceName, intReturn Dim objWMI, objServices, objService ' ' Get the WMI service ' strComputer = "localhost" Set objWMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _ strComputer & "\root\cimv2") ' ' Specify the service name ' strServiceName = "Remote Registry" ' ' Get the service instance ' Set objServices = objWMI.ExecQuery("SELECT * FROM Win32_Service " & _ "WHERE DisplayName = '" & strServiceName & "'") For Each objService In objServices ' ' Save the service name ' strServiceName = objService.DisplayName ' ' Is the service started? ' If objService.Started Then ' ' Can it be stopped? ' If objService.AcceptStop Then ' ' Attempt to stop the service ' intReturn = objService.StopService ' ' Check the return value ' If intReturn <> 0 Then ' ' Display the error message ' WScript.Echo "ERROR: The " & strServiceName & " service " & _ "failed to stop. The return code is " & intReturn Else ' ' Display the current state ' WScript.Echo "The " & strServiceName & " service is now " & _ objService.State End If Else ' ' Display the error message ' WScript.Echo "ERROR: The " & strServiceName & " service " & _ "cannot be stopped." End If Else ' ' Attempt to start the service ' intReturn = objService.StartService ' ' Check the return value ' If intReturn <> 0 Then ' ' Display the error message ' WScript.Echo "ERROR: The " & strServiceName & " service " & _ "failed to start. The return code is " & intReturn Else ' ' Display the current state ' WScript.Echo "The " & strServiceName & " service is now " & _ objService.State End If End If Next ' ' Release the objects ' Set objWMI = Nothing Set objServices = Nothing Set objService = Nothing
|
This script gets the WMI service object and uses its ExecQuery method to return an instance of the Win32_Service class by using the WHERE clause to look for a specific service name. That name was earlier stored in the strServiceName variable. In the For Each...Next loop, the script first checks to see whether the service is currently started by checking its Started property:
If the Started property returns True, the service is running, so we want to stop it. The script then checks the service’s AcceptStop property, which returns False for essential Windows 7 services that can’t be stopped. In this case, the script returns an error message. If AcceptStop returns True, the script attempts to stop the service by running the StopService method.
If the Started property returns False, the service is stopped, so we want to start it. The script attempts to start the service by running the StartService method.
The StopService and StartService methods generate the return codes shown in Table 2.
Table 2. Return Codes Generated by the StartService and StopService Methods
Return Code | Description |
---|
0 | Success |
1 | Not supported |
2 | Access denied |
3 | Dependent services running |
4 | Invalid service control |
5 | Service cannot accept control |
6 | Service not active |
7 | Service request timeout |
8 | Unknown failure |
9 | Path not found |
10 | Service already stopped |
11 | Service database locked |
12 | Service dependency deleted |
13 | Service dependency failure |
14 | Service disabled |
15 | Service logon failed |
16 | Service marked for deletion |
17 | Service no thread |
18 | Status circular dependency |
19 | Status — duplicate name |
20 | Status — invalid name |
21 | Status — invalid parameter |
22 | Status — invalid service account |
23 | Status — service exists |
24 | Service already paused |
For both the StopService and StartService methods, the script stores the return code in the intReturn
variable and then checks to see whether it’s a number other than 0. If
so, the script displays an error message that includes the return code;
otherwise, the script displays the new state of the service (as given
by the State property).